package com.uniontech.template.controller;

//import com.github.tobato.fastdfs.domain.fdfs.StorePath;
//import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.uniontech.template.entity.FileEntity;
import com.uniontech.template.service.SysFileEntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping(value = "/file", name = "文件上传")
public class FileController {

//    @Autowired
//    private FastFileStorageClient storageClient;

    @Autowired
    private SysFileEntityService fileEntityService;

    /**
     * 文件上传
     *
     * @param file
     * @return
     * @throws IOException
     */
    @ResponseBody
    @PostMapping(value = "/upload", name = "文件上传")
    public String upload(@RequestParam(value = "file", required = false) MultipartFile file) {
        String url = "http://localhost:8000/files/";
        try {
            // 获取文件全名
            String fileName = file.getOriginalFilename();
            String extName = null;
            String Name = null;
            // 设置存放地址
            String path = "/home/lijialei/Desktop/dir/htmlworkspace/test/public/files/";
            if (!fileName.contains(".")) {
                fileName += ".blank";
            }
            // 获取后缀名
            extName = fileName.substring(fileName.lastIndexOf('.'));
            // 获取除去后缀文件名
            Name = fileName.substring(0,fileName.lastIndexOf('.'));
            // 获取文件
            FileEntity fileEntity=fileEntityService.getFileEntityByFileName(fileName);
            // 查询是否已经存在该文件，不排除已删除文件
            if(fileEntity != null) {
                // 获取数据库中最新文件名
                fileName = fileEntityService.getLastFileNameByName(Name, extName);
                // System.out.println("1:"+fileName);
                // 已经有了序号的
                if(fileName.contains(").")){
                    // 获取括号中数字
                    int replyNum = Integer.parseInt(fileName.substring(fileName.lastIndexOf('(')+1, fileName.lastIndexOf(')')));
                    Name = Name.substring(0,fileName.lastIndexOf('('));
                    Name += "("+(replyNum+1)+")";
                    fileName = Name+extName;
                    // System.out.println("2:"+fileName);
                // 还没有序号的
                } else {
                    Name += "(1)";
                    fileName=Name+extName;
                    // System.out.println("3:"+fileName);
                }
            }
            // 设置http地址
            url += fileName;
            // 设置本地路径
            path += fileName;
                // 文件下载到本地
                file.transferTo(new File(path));
                // 创建文件实例
                fileEntity = new FileEntity();
                // 获取文件路径
                fileEntity.setFilePath(path);
                // 获取文件地址
                fileEntity.setFileUrl(url);
                // 获取文件名
                fileEntity.setFileName(fileName);
                // 获取文件大小
                fileEntity.setFileLen(file.getSize());
                // 获取文件扩展名
                fileEntity.setExtName(extName);
                // 获取创建时间
                fileEntity.setCreateTime(LocalDateTime.now());
                // 获取删除状态
                fileEntity.setDelFlg(1);
                // 存入数据库
                fileEntityService.addFileEntity(fileEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url ;
    }

    @ResponseBody
    @RequestMapping (value = "/list", name = "文件列表")
    public List<FileEntity> list(){
        List<FileEntity> list = fileEntityService.getFileList();
        return list;
    }

    @ResponseBody
    @PostMapping(value = "/delete", name = "删除文件")
    public boolean delete(@RequestParam Integer fileId) throws Exception{
        boolean status = fileEntityService.deleteFile(fileId);
        if(status){
            return true;
        }
            return false;
        /*FileEntity fileEntity;
        if(status){
            fileEntity = fileEntityService.getFileEntityById(fileId);
            String path = fileEntity.getFilePath();
            File delFile = new File(path);
            if(delFile.isFile() && delFile.exists()){
                delFile.delete();
                return true;
            }else{
                throw new Exception("本地文件不存在");
            }
        }else{
            return false;
        }*/
    }
}
